/**
 * Advisors 模块 API
 * 项目优化建议相关接口（实验性）
 */

import { request, type ApiResponse } from "../request";

// ============ 类型定义 ============

/**
 * Lint 名称枚举
 */
export type LintName =
	| "unindexed_foreign_keys"
	| "auth_users_exposed"
	| "auth_rls_initplan"
	| "no_primary_key"
	| "unused_index"
	| "multiple_permissive_policies"
	| "policy_exists_rls_disabled"
	| "rls_enabled_no_policy"
	| "duplicate_index"
	| "security_definer_view"
	| "function_search_path_mutable"
	| "rls_disabled_in_public"
	| "extension_in_public"
	| "rls_references_user_metadata"
	| "materialized_view_in_api"
	| "foreign_table_in_api"
	| "unsupported_reg_types"
	| "auth_otp_long_expiry"
	| "auth_otp_short_length"
	| "ssl_not_enforced"
	| "network_restrictions_not_set"
	| "password_requirements_min_length"
	| "pitr_not_enabled"
	| "auth_leaked_password_protection"
	| "auth_insufficient_mfa_options"
	| "auth_password_policy_missing"
	| "leaked_service_key"
	| "no_backup_admin"
	| "vulnerable_postgres_version";

/**
 * Lint 级别
 */
export type LintLevel = "ERROR" | "WARN" | "INFO";

/**
 * Lint 类型
 */
export type LintType = "table" | "view" | "auth" | "function" | "extension" | "compliance";

/**
 * Lint 元数据
 */
export interface LintMetadata {
	schema?: string;
	name?: string;
	entity?: string;
	type?: LintType;
	fkey_name?: string;
	fkey_columns?: number[];
}

/**
 * Lint 项
 */
export interface Lint {
	name: LintName;
	title: string;
	level: LintLevel;
	facing: "EXTERNAL";
	categories: string[];
	description: string;
	detail: string;
	remediation: string;
	metadata?: LintMetadata;
	cache_key: string;
}

/**
 * 项目建议响应
 */
export interface ProjectAdvisorsResponse {
	lints: Lint[];
}

/**
 * 安全建议查询参数
 */
export interface SecurityAdvisorsParams {
	lint_type?: "sql";
}

// ============ API 方法 ============

/**
 * 获取项目性能建议（实验性）
 */
export function getPerformanceAdvisors(
	ref: string
): Promise<ApiResponse<ProjectAdvisorsResponse>> {
	return request.get<ProjectAdvisorsResponse>(
		`/v1/projects/${ref}/advisors/performance`
	);
}

/**
 * 获取项目安全建议（实验性）
 */
export function getSecurityAdvisors(
	ref: string,
	params?: SecurityAdvisorsParams
): Promise<ApiResponse<ProjectAdvisorsResponse>> {
	return request.get<ProjectAdvisorsResponse>(
		`/v1/projects/${ref}/advisors/security`,
		params
	);
}

export default {
	getPerformanceAdvisors,
	getSecurityAdvisors
};
